using System;

namespace SharpMud.MudLib.Core.Time
{
	public class MudTimeSystem
	{
		#region Static Globalization
		private static MudTimeSystem _Current;
		public static MudTimeSystem Current
		{
			get
			{
				return _Current;
			}
		}
		public void GoGlobal()
		{
			_Current = this;
		}

		#endregion
		
		private MudDateTime			_MudTimeOrigin;
		private System.DateTime		_TimeWentUp;
		private bool _Enabled;

		public MudDateTime CurrentMudTime
		{
			get
			{
				if(_Enabled==true)
				{
					return _MudTimeOrigin.Add((MudTimeSpan)(DateTime.Now.Subtract(_TimeWentUp)));
				}
				else
				{
					return MudTimeConfiguration.Current.MudTimeOrigin.Add((MudTimeSpan)MudTimeConfiguration.Current.TotalMudUptime);
				}
			}
		}

		public void Enable()
		{
			if(_Enabled==true) return;

			_MudTimeOrigin = _MudTimeConfig.MudTimeOrigin.Add(((MudTimeSpan)_MudTimeConfig.TotalMudUptime_TS));
			_TimeWentUp = System.DateTime.Now;
			
			_Enabled = true;
		}
		public void Disable()
		{
			if(_Enabled==false) return;

			_MudTimeConfig.TotalMudUptime_TS = _MudTimeConfig.TotalMudUptime_TS.Add(DateTime.Now.Subtract(_TimeWentUp));

			_Enabled = false;
		}

	
		public bool Enabled
		{
			get
			{
				return _Enabled;
			}
			set
			{
				if(_Enabled==true && value==false)
				{
					Disable();
				}
				else if (_Enabled==false && value==true)
				{
					Enable();
				}
			}
		}

		private MudTimeConfiguration				_MudTimeConfig;

		public MudTimeSystem(MudTimeConfiguration mudTimeConfig)
		{
			_MudTimeConfig = mudTimeConfig;			
		}
	}
}
